{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "scientific-priest", "metadata": {}, "outputs": [], "source": [ "# This tutorial will introduce simple implementations of while loops\n", "# in Python." ] }, { "cell_type": "code", "execution_count": 2, "id": "promising-witness", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n = 4\n", "n! = 24\n" ] } ], "source": [ "# Here's a while loop that calculates the factorial of n. Start with n = 4\n", "# to make sure things are working properly. The loop executes until n > 1.\n", "# That is, the the last iteration of the loop will occur when n = 2. (You\n", "# could actually let the while loop run until n = 1 since factorial*1 =\n", "# factorial).\n", "n = 4\n", "print('n =', n)\n", "factorial = 1\n", "while n > 1:\n", " factorial = factorial*n\n", " n = n - 1\n", "print('n! =', factorial)" ] }, { "cell_type": "code", "execution_count": 3, "id": "handed-curtis", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n = 50\n", "n! = 30414093201713378043612608166064768844377641568960512000000000000\n" ] } ], "source": [ "# Here's a more interesting factorial\n", "n = 50\n", "print('n =', n)\n", "factorial = 1\n", "while n > 1:\n", " factorial = factorial*n\n", " n = n - 1\n", "print('n! =', factorial)" ] }, { "cell_type": "code", "execution_count": 4, "id": "stone-surgeon", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n! = 3.0414093201713376e+64\n" ] } ], "source": [ "# We can force the number to be written in scientific notation while keeping\n", "# only some of the digits after the decimal point using:\n", "print('n! =', float(factorial))" ] }, { "cell_type": "code", "execution_count": 5, "id": "colonial-nickel", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n = 170\n", "n! = 7.257415615307999e+306\n" ] } ], "source": [ "# Here's a a crazy big number...\n", "n = 170\n", "print('n =', n)\n", "factorial = 1\n", "while n > 1:\n", " factorial = factorial*n\n", " n = n - 1\n", "print('n! =', float(factorial))" ] }, { "cell_type": "code", "execution_count": 6, "id": "underlying-cocktail", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter an integer: 17\n", "n = 17\n", "n! = 355687428096000.0\n" ] } ], "source": [ "# Here's the factorial of a user-entered value.\n", "n = int(input(\"Enter an integer: \")) \n", "print('n =', n)\n", "factorial = 1\n", "while n > 1:\n", " factorial = factorial*n\n", " n = n - 1\n", "print('n! =', float(factorial))" ] }, { "cell_type": "code", "execution_count": 7, "id": "minus-publicity", "metadata": {}, "outputs": [], "source": [ "# Maybe you want to the user to be able to enter only integer numbers with\n", "# appropriate error messages. \n", "\n", "# See the nested control structure tutorial for an implementation of this\n", "# check. That tutorial will also show you how to calculate the factorials\n", "# of a bunch of n values and then plot the results vs n." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }